<?php 
/*
 * Created on 2009-7-19
 *
 * Author: guobin@yizin.net
 */

require_once (dirname(__FILE__)."/services.php");
require_once (dirname(__FILE__)."/helper.php");
require_once ("platform/Interceptor.php");

$resourceBase = RESOURCE_BASE."site/";

define('ADMIN_HEADER', Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_admin_header_.php"));

define('ADMIN_FOOTER', Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_admin_footer_.php"));

define('SITE_HEADER', Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_site_header_.php"));

define('SITE_FOOTER', Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_site_footer_.php"));

class com_yizin_site_RouterExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/$}'] = dirname(__FILE__)."/actions.php";
		$context['rule']['{^/home/$}'] = dirname(__FILE__)."/actions.php";
		$context['rule']['{^/about/$}'] = dirname(__FILE__)."/actions.php";
		$context['rule']['{^/contact/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/login/(.+)?$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/logout/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/account/.*$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/register/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/forget/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/user/(page/([^/]+)/)?$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/user/(all|id|email|nickname|role)/.*/(page/([^/]+)/)?$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/user/new/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/user/create/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/user/delete/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/user/[^/]+/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/profile/[^/]+/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/(page/([^/]+)/)?$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/new/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/create/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/delete/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/grant/.*$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/[^/]+/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/admin/user/role/[^/]+/privilege/$}'] = dirname(__FILE__)."/actions.php";
        $context['rule']['{^/member/.*/$}'] = dirname(__FILE__)."/actions.php";
        
        $chain->next($context);
    }
}

class com_yizin_site_BaseInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        $GLOBALS['currentUser'] = session_getCurrentUser();
        
        $GLOBALS['currentClient'] = session_getCurrentClient();
        
        $chain->next($context);
    }
}

class com_yizin_site_AdminInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        $GLOBALS['header'] = $header;
        $GLOBALS['footer'] = $footer;
        $GLOBALS['menu'] = $menu;
        $chain->next($context);
    }
}
class com_yizin_site_user_AdminInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
    
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
        
            $userContext = array("user"=>$currentUser['serial']);
            $featureManager->apply("com.yizin.platform.role.get.by.user", $userContext);
            $roleContext = array('role'=>$userContext['role']['serial'], 'id'=>'user.user');
            $featureManager->apply("com.yizin.platform.privilege.get.by.id", $roleContext);
            if (!($roleContext['allow'])) {
                $mvc = new MVC(array());
                $mvc->setMessage("message", "帐户权限不足");
                $currentURI = Utils::getURI();
                $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
            }
            ;
        }
        $chain->next($context);
    }
}
class com_yizin_site_LoginInterceptor extends Interceptor {

    public function intercept(&$context, InterceptorChain $chain) {
    
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $chain->next($context);
        } else {
            $mvc = new MVC(array());
            $mvc->setMessage("message", "请先登录");
            $currentURI = Utils::getURI();
            $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
        }
    }
}
class com_yizin_site_SiteInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        $GLOBALS['header'] = $header;
        $GLOBALS['footer'] = $footer;
        $GLOBALS['menu'] = $menu;
        $GLOBALS['control'] = $control;
        
        $chain->next($context);
    }
}

class com_yizin_site_InterceptorExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/.*$}'][] = new com_yizin_site_BaseInterceptor();
        $context['rule']['{^/admin/.*$}'][] = new com_yizin_site_AdminInterceptor();
        $context['rule']['{^/admin/user/.*$}'][] = new com_yizin_site_user_AdminInterceptor();
        $context['rule']['{^/(?!admin).*$}'][] = new com_yizin_site_SiteInterceptor();
        $context['rule']['{^/admin/user/.*$}'][] = new com_yizin_site_LoginInterceptor();
        $context['rule']['{^/admin/$}'][] = new com_yizin_site_LoginInterceptor();
        $chain->next($context);
    }
}

class com_yizin_site_AdminHeaderHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        if (!$context['layout']) {
            $context['layout'] = "2x";
        }
        $context['html'] = sprintf(ADMIN_HEADER, $context['title'], $context['head'], $context['page_id'], $context['layout']);
    }
}

class com_yizin_site_AdminFooterHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['html'] = sprintf(ADMIN_FOOTER);
    }
}

class com_yizin_site_AdminMenuHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['html'] = $this->renderMenu($context);
    }
    
    private function renderMenu($context) {
        $this->doRenderMenu($context[items], $wrapper, "menu", 0, $context);
        return $wrapper['html'];
    }
    
    private function doRenderMenu($items, &$wrapper, $parentId, $level, $context) {
        if ($items && is_array($items)) {
            if ($level == 0) {
                $wrapper['html'] .= '<div class="ysi-menu"><ul>';
                foreach ($items as $key=>$item) {
                    $id = "$parentId.$key";
                    $current = substr($context['current'], 0, strpos($context['current'], '.'));
                    $liClass = "";
                    $liClass .= count($item['items']) ? 'has-submenu ' : '';
                    $liClass .= "menu.$current" == $id ? 'current ' : '';
                    $liClass = strrpos($liClass, ' ') == strlen($liClass) - 1 ? substr($liClass, 0, strlen($liClass) -
                         1) : $liClass;
                    $wrapper['html'] .= '<li'.($liClass != "" ? ' class="'.$liClass.'"' : '').'>';
                    $wrapper['html'] .= '<div class="ysi-menu-icon"></div>';
                    $wrapper['html'] .= '<div class="ysi-menu-toggle"></div>';
                    $wrapper['html'] .= '<a class="ysi-menu-link" id="'.$id.'" href="'.$item['href'].'">';
                    $wrapper['html'] .= $item['text'];
                    $wrapper['html'] .= '</a>';
                    $this->doRenderMenu($item['items'], $wrapper, $id, $level + 1, $context);
                    $wrapper['html'] .= '</li>';
                }
                $wrapper['html'] .= "</ul></div>";
            } elseif ($level == 1) {
                $wrapper['html'] .= '<div class="ysi-menu-submenu"><ul>';
                foreach ($items as $key=>$item) {
                    $id = "$parentId.$key";
                    $current = $context['current'];
                    $wrapper['html'] .= '<li'.("menu.$current" == $id ? ' class="current"' : '').'>';
                    $wrapper['html'] .= '<a class="ysi-menu-submenu-link" id="'.$id.'" href="'.$item['href'].'">';
                    $wrapper['html'] .= $item['text'];
                    $wrapper['html'] .= '</a>';
                    $this->doRenderMenu($item['items'], $wrapper, $id, $level + 1, $context);
                    $wrapper['html'] .= '</li>';
                }
                $wrapper['html'] .= "</ul></div>";
            }
        }
    }
}

class com_yizin_site_AdminMenuHandler1 extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['html'] = $this->renderMenu($context)."<hr />";
    }
    
    private function renderMenu($context) {
        $this->doRenderMenu($context[items], $wrapper, "menu", $context);
        return $wrapper['html'];
    }
    
    private function doRenderMenu($items, &$wrapper, $parentId, $context) {
        if ($items && is_array($items)) {
            $wrapper['html'] .= '<ul>';
            foreach ($items as $key=>$item) {
                $id = "$parentId.$key";
                $current = $context['current'];
                $wrapper['html'] .= '<li>';
                $wrapper['html'] .= '<a id="'.$id.'" href="'.$item['href'].'">';
                if ("menu.$current" == $id) {
                    $wrapper['html'] .= "<strong>";
                }
                $wrapper['html'] .= $item['text'];
                if ("menu.$current" == $id) {
                    $wrapper['html'] .= "</strong>";
                }
                $wrapper['html'] .= '</a>';
                $this->doRenderMenu($item['items'], $wrapper, $id, $context);
                $wrapper['html'] .= '</li>';
            }
            $wrapper['html'] .= "</ul>";
        }
    }
}


class com_yizin_site_SiteHeaderHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        if (!$context['layout']) {
            $context['layout'] = "28";
        }
		$toolbarClass = $context['hasToolBar'] == TRUE?'class="has-tool-bar"':"";
        $context['html'] = sprintf(SITE_HEADER, $context['title'], $context['head'], $context['page_id'], $context['layout'], $toolbarClass, $context['intro']);
    }
}


class com_yizin_site_SiteFooterHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['html'] = sprintf(SITE_FOOTER);
    }
}


class com_yizin_site_SiteMenuHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['html'] = $this->renderMenu($context);
    }
    
    private function renderMenu($context) {
        $this->doRenderMenu($context[items], $wrapper);
        return $wrapper['html'];
    }
    
    private function doRenderMenu($items, &$wrapper) {
        if ($items && is_array($items)) {
            $wrapper['html'] .= '<ul>';
            foreach ($items as $item) {
                $wrapper['html'] .= '<li>';
                $wrapper['html'] .= '<a href="'.$item['href'].'">'.$item['text'].'</a>';
                $this->doRenderMenu($item['items'], $wrapper);
                $wrapper['html'] .= '</li>';
            }
            $wrapper['html'] .= '</ul>';
        }
    }
}

class com_yizin_site_AccountMenuHandler extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $items = $context['items'];
        if ($items && is_array($items)) {
            $current = $context['current'];
            $context['html'] .= '
			<div class="ysi-tabs"><h3 class="ysi-tabs-title">个人设置</h3>
			<ul class="ysi-tabs-heading">';
            foreach ($items as $key=>$item) {
                if ($current == $key) $context['html'] .= '<li class="current">';
                else $context['html'] .= '<li>';
                $context['html'] .= '<a href="'.$item['href'].'">'.$item['text'].'</a>';
                $context['html'] .= '</li>';
            }
            $context['html'] .= '<div class="clear"></div></ul></div>';
        }
    }
}

class com_yizin_site_AdminMenuExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['user']['href'] = REQUEST_BASE."admin/user/user/";
        $context['items']['user']['text'] = "用户管理";
        $context['items']['user']['items']['user']['href'] = REQUEST_BASE."admin/user/user/";
        $context['items']['user']['items']['user']['text'] = "用户管理";
        $context['items']['user']['items']['role']['href'] = REQUEST_BASE."admin/user/role/";
        $context['items']['user']['items']['role']['text'] = "角色管理";
        $chain->next($context);
    }
}

class com_yizin_site_SiteMenuExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['index']['href'] = REQUEST_BASE;
        $context['items']['index']['text'] = "首页";
        $chain->next($context);
    }
}


class com_yizin_site_SiteControlExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        global $currentUser;
        if (!$currentUser) {
        	global $redirect, $login_error;
			$login_error = $context['login_error'];
            $login = Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_site_login_.php");
            $context['html'] = $login;
        } else {
        	global $account_current;
			$account_current = $context['current'];
            $control = Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_site_control_.php");
			$context['html'] = $control;
        }
        $chain->next($context);
    }
}

class com_yizin_site_AccountMenuExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['summary']['href'] = REQUEST_BASE."account/summary/";
        $context['items']['summary']['text'] = "概要信息";
        $context['items']['profile']['href'] = REQUEST_BASE."account/profile/";
        $context['items']['profile']['text'] = "基本资料";
        $context['items']['password']['href'] = REQUEST_BASE."account/password/";
        $context['items']['password']['text'] = "密码设置";
        $chain->next($context);
    }
}

class com_yizin_site_PrivilegeExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['user']['text'] = "用户管理";
        $context['items']['user']['items']['user']['text'] = "用户管理";
        $context['items']['user']['items']['role']['text'] = "角色管理";
        $chain->next($context);
    }
}

class com_yizin_user_GetUserExtension extends Extension {

    public function apply(&$context, Chain $chain) {
        $context['data'] = $context['data'] ? $context['data'] : array();
        $context['data']['member'] = array();
        if (count($context['member'])) {
            $serialList = $context['member'];
            $memberList = user_getByField("serial", $serialList);
            var_dump($memberList);
            foreach ($memberList as $user) {
                $user['href'] = REQUEST_BASE."member/".$user['serial']."/";
                $context['data']['member'][$user['serial']] = $user;
            }
        }
        ;
        
        $chain->next($context);
    }
}

class com_yizin_site_UserGetByFieldService extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['user'] = user_getByField($context['name'], $context['value']);
        if (is_array($context['value'])) {
            foreach ($context['user'] as & $user) {
                $user['href'] = REQUEST_BASE."member/".$user['serial']."/";
                $path = Utils::getPathBySerial($user['serial'], true);
                if (Utils::isBlank($user['image'])) {
                    $user['image'] = RESOURCE_BASE."site/images/user_face_default.gif";
                } else {
                    $user['image'] = RESOURCE_BASE."com.yizin.site/".$path.$user['image'];
                }
            }
        } else {
            $user = &$context['user'];
            $user['href'] = REQUEST_BASE."member/".$user['serial']."/";
            $path = Utils::getPathBySerial($user['serial'], true);
            if (Utils::isBlank($user['image'])) {
                $user['image'] = RESOURCE_BASE."site/images/user_face_default.gif";
            } else {
                $user['image'] = RESOURCE_BASE."com.yizin.site/".$path.$user['image'];
            }
        }
    }
}

class com_yizin_site_RoleGetByUserService extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['role'] = role_getByUser($context['user']);
    }
}

class com_yizin_site_new_user_Extension extends Extension {
    public function apply(&$context, Chain $chain) {
        global $t_title,$t_userList;
        $t_title = "新加入的用户";
        $t_userList = user_getAll(0, 9, "register_time desc");
        if (count($t_userList)) {
            $context["html"] .= Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_user_right_list_.php");
        }
        $chain->next($context);
    }
}

class com_yizin_site_last_user_Extension extends Extension {
    public function apply(&$context, Chain $chain) {
        global $t_title,$t_userList;
        $t_title = "最近登录的用户";
        $t_userList = user_getAll(0, 9, "last_access_time desc");
        
        if (count($t_userList)) {
            $context["html"] .= Utils::loadSegment(dirname(__FILE__)."/views/segments/_s_user_right_list_.php");
        }
        $chain->next($context);
    }
}

class com_yizin_site_PrivilegeGetByIdService extends Extension {
    public function apply(&$context, Chain $chain) {
        $chain->next($context);
        $context['privilege'] = privilege_getById($context['role'], $context['id']);
        $context['allow'] = $context['privilege'] == "allow";
    }
}


class com_yizin_site_BreadcrumbExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['breadcrumb_trail'] = array();
        $chain->next($context);
    }
}


class com_yizin_site_SiteFeature extends Feature {
    public function registerExtension() {
        $this->extensions = Array( new com_yizin_site_AdminHeaderHandler('com.yizin.platform.admin.header',
             "",
             0),
                      new com_yizin_site_AdminFooterHandler('com.yizin.platform.admin.footer', "", 0),
                      new com_yizin_site_AdminMenuHandler("com.yizin.platform.admin.menu", "", 0),
                      new com_yizin_site_SiteHeaderHandler('com.yizin.platform.site.header', "", 0),
                      new com_yizin_site_SiteFooterHandler('com.yizin.platform.site.footer', "", 0),
                      new com_yizin_site_SiteMenuHandler("com.yizin.platform.site.menu", "", 0),
                      new com_yizin_site_AccountMenuHandler("com.yizin.platform.account.menu", "", 0),
                      new com_yizin_site_RouterExtension("com.yizin.platform.router"),
                      new com_yizin_site_InterceptorExtension("com.yizin.platform.interceptor"),
                      new com_yizin_site_AdminMenuExtension("com.yizin.platform.admin.menu"),
                      new com_yizin_site_SiteMenuExtension("com.yizin.platform.site.menu"),
                      new com_yizin_site_SiteControlExtension("com.yizin.platform.site.control"),
                      new com_yizin_site_AccountMenuExtension("com.yizin.platform.account.menu"),
                      new com_yizin_site_PrivilegeExtension("com.yizin.platform.privilege"),
                      new com_yizin_site_UserGetByFieldService("com.yizin.platform.user.get.by.field", "", 0),
                      new com_yizin_site_RoleGetByUserService("com.yizin.platform.role.get.by.user", "", 0),
                      new com_yizin_site_PrivilegeGetByIdService("com.yizin.platform.privilege.get.by.id",
                           "",
                           0),
                      new com_yizin_site_BreadcrumbExtension("com.yizin.site.breadcrumb"),
                      new com_yizin_site_last_user_Extension("com.yizin.book.show.right"),
                      new com_yizin_site_new_user_Extension("com.yizin.book.show.right"));
    }
    
    public function registerExtendable() {
        $this->extendables = Array( new Extendable("com.yizin.platform.router", "
URL request router
Structure: 
Array(
  'rule' => Array('{^/test/$}' => 'test.php')
)"), new Extendable("com.yizin.platform.interceptor", "
URL request interceptor
Structure: 
Array(
  'rule' => Array('{^/test/$}' => (instance of com_yizin_platform_Interceptor)),
  'end' => (a function called at the end)
)"), new Extendable("com.yizin.platform.admin.header", "
Admin header
Structure:
Array(
  'html' => ''
)"), new Extendable("com.yizin.platform.admin.footer", "
Admin footer
Structure:
Array(
  'html' => ''
)"), new Extendable("com.yizin.platform.admin.menu", "
Admin menu
Structure:
Array(
  'items' => Array(
    'itemId1' => Array(
      'href' => '',
      'text' => '',
      'items' => *recursion*
    ),
    'itemId2' => Array(
      'href' => '',
      'text' => '',
      'items' => *recursion*
    )
  ),
  'html' => ''
"), new Extendable("com.yizin.platform.site.menu", "
Site menu
Structure:
Array(
  'items' => Array(
    'itemId1' => Array(
      'href' => '',
      'text' => '',
      'items' => *recursion*
    ),
    'itemId2' => Array(
      'href' => '',
      'text' => '',
      'items' => *recursion*
    )
  ),
  'html' => ''
)"), new Extendable("com.yizin.platform.site.control", "
Site menu
Structure:
Array(
  'items' => Array(
    'itemId1' => Array(
      'href' => '',
      'text' => ''
    ),
    'itemId2' => Array(
      'href' => '',
      'text' => ''
    )
  ),
  'html' => ''
)"), new Extendable("com.yizin.platform.account.menu", "
Site menu
Structure:
Array(
  'items' => Array(
    'itemId1' => Array(
      'href' => '',
      'text' => ''
    ),
    'itemId2' => Array(
      'href' => '',
      'text' => ''
    )
  ),
  'html' => ''
)"));
    }
    
    public function init() {
        $conn = getDBConnection();
        $conn->query("
		drop table if exists t_user;
		");
        $conn->query("
		create table t_user
		(
			serial bigint primary key auto_increment,
			id varchar(50),
			email varchar(80),
			nickname varchar(80),
			password varchar(80),
			description text,
			register_time datetime,
			last_access_time datetime,
			before_last_access_time datetime,
			image varchar(255),
			index (id),
			index (email)
		) CHARSET=utf8 AUTO_INCREMENT = 100001;
		");
        //        $conn->query("
        //		insert into t_user (
        //			serial,
        //			id ,
        //			email ,
        //			nickname,
        //			password ,
        //			description ,
        //			register_time ,
        //			last_access_time ,
        //			before_last_access_time ,
        //			image
        //		) values (
        //			'100001',
        //			'admin',
        //			 NULL ,
        //			 NULL ,
        //			 '123456',
        //			 NULL ,
        //			 NULL ,
        //			 NULL ,
        //			 NULL ,
        //			 NULL
        //		);");
        $conn->query("
		drop table if exists t_user_profile;
		");
        $conn->query("
		create table t_user_profile
		(
			user_serial bigint not null,
			profile_key varchar(50) not null,
			profile_value text not null,
			
			primary key (user_serial, profile_key),
			foreign key (user_serial) references t_user (serial)
		) CHARSET=utf8 AUTO_INCREMENT = 100001;
		");
        $conn->query("
		drop table if exists t_role;
		");
        $conn->query("
		create table t_role
		(
			serial bigint primary key auto_increment,
			id varchar(50),
			name varchar(80),
			description varchar(100)
		) CHARSET=utf8 AUTO_INCREMENT = 100001;
		");
        $conn->query("
		insert into t_role (id, name, description) values
			('admin','管理员',''),
			('user','用户',''),
			('guest','访客','');
		");
        $conn->query("
		drop table if exists t_mapping_user_role;
		");
        $conn->query("
		create table t_mapping_user_role
		(
			user_serial bigint primary key,
			role_serial bigint
		);
		");
        $conn->query("
		insert into t_mapping_user_role
		(
			user_serial, 
			role_serial
		)
		values(
			'100001',
			'100001'
		);");
        $conn->query("
		drop table if exists t_privilege;
		");
        $conn->query("
		create table t_privilege
		(
			role_serial bigint primary key,
			allow text,
			deny text,
			`default` boolean
		);
		");
        $conn->query("
		insert into t_privilege
		(
			role_serial,
			allow,
			deny,
			`default`
		)values(
			'100001',
			NULL,
			NULL,
			'1'
		);
		");
        $conn->disconnect();
        //File Directory
        $resourcepath = RESOURCE_PATH.DIRECTORY_SEPARATOR.'com.yizin.site';
        try {
            if (mkdir($resourcepath)) {
            } else {
                throw new Exception("建立文件夹失败");
            }
        }
        catch(exception $e) {
            echo $e->getmessage();
        }
    }
    
    public function destroy() {
        $conn = getDBConnection();
        
        $conn->query("drop table if exists t_user;");
        $conn->query("drop table if exists t_user_profile;");
        $conn->query("drop table if exists t_role;");
        $conn->query("drop table if exists t_mapping_user_role;");
        $conn->query("drop table if exists t_privilege;");
        
        $conn->disconnect();
        //File Directory
        $resourcepath = RESOURCE_PATH.DIRECTORY_SEPARATOR.'com.yizin.site';
        if (is_dir($resourcepath)) {
            try {
                if (Utils::removeDir($resourcepath)) {
                } else {
                    throw new Exception("删除文件夹失败");
                }
            }
            catch(exception $e) {
                echo $e->getmessage();
            }
        }
    }
}

$featureClass = "com_yizin_site_SiteFeature";
$featureName = "SiteFeature";
$featureIntroduction = "Site feature";
?>
